// 获取应用实例
const app = getApp();
console.log(app, ";;;");
import { login } from "./../../util/http"
Page({
  data: {
    toast: {
      hidden: false,
      icon: "download",
      msg: "请求超时",
    },
    userInfo: {},
    hasLogin: wx.getStorageSync("loginFlag") ? true : false,

    /**
     * .wxss 文件中，直接引用本地图片时，真机预览时会无法显示
     * 解决方案
     * 方案一：使用网络图片
     * 方案二：将本地图片转换为 base64
     * 方案三：使用 image 组件
     */
    // 可在wxml文件中用内联样式或者image标签都可以引用本地的图片
    // 相对路径去访问图片，可以用style样式的方式或者image标签
    barList: [
      {
        url: "/page/tab-demo/index",
        icon: "/images/icon/constomer-icon.png",
        text: "tab demo",
      },
      {
        url: "/page/form-demo/index",
        icon: "/images/icon/constomer-icon.png",
        text: "form demo",
      },
      {
        url: "/page/chart-demo/index",
        icon: "/images/icon/constomer-icon.png",
        text: "chart demo",
      },
      {
        url: "/page/upload-demo/index",
        icon: "/images/icon/constomer-icon.png",
        text: "upload demo",
      },
      {
        url: "/page/accordion-demo/index",
        icon: "/images/icon/constomer-icon.png",
        text: "accordion demo",
      },
      {
        url: "/page/progress-demo/index",
        icon: "/images/icon/constomer-icon.png",
        text: "progress[canvas] demo",
      },
      {
        url: "/page/password-demo/index",
        icon: "/images/icon/constomer-icon.png",
        text: 'password[focus="true"] demo',
      },
      {
        url: "/page/delete-demo/index",
        icon: "/images/icon/constomer-icon.png",
        text: "slide delete",
      },
      {
        url: "/page/scroll-demo/index",
        icon: "/images/icon/constomer-icon.png",
        text: "scroll demo",
      },
      {
        url: "/page/modal-demo/index",
        icon: "/images/icon/constomer-icon.png",
        text: "modal demo",
      },
      {
        url: "/page/preview-demo/index",
        icon: "/images/icon/constomer-icon.png",
        text: "preview demo",
      },
      {
        url: "/page/activity/index",
        icon: "/images/icon/constomer-icon.png",
        text: "activity demo",
      },
      {
        url: "/page/button-demo/index",
        icon: "/images/icon/constomer-icon.png",
        text: "button demo",
      },
      {
        url: "/page/wxs-demo/index",
        icon: "/images/icon/constomer-icon.png",
        text: "wxs demo",
      },
      {
        url: "/page/for-demo/index",
        icon: "/images/icon/constomer-icon.png",
        text: "wx:for",
      },
      {
        url: "/page/map/index",
        icon: "/images/icon/constomer-icon.png",
        text: "map demo",
      },
      {
        url: "/page/map-demo/index",
        icon: "/images/icon/constomer-icon.png",
        text: "qq map demo",
      },
      {
        url: "/page/person/address/index",
        icon: "/images/icon/constomer-icon.png",
        text: "我的地址",
      },
      {
        url: "/page/contact-demo/index",
        icon: "/images/icon/constomer-icon.png",
        text: "客服消息",
      },
      {
        url: "/page/video-demo/index",
        icon: "/images/icon/constomer-icon.png",
        text: "视频",
      },
      {
        url: "/packageDemo/page/home/index",
        icon: "/images/icon/constomer-icon.png",
        text: "分包",
      },
    ],
  },

  fetchUserInfo(e) {
    const params = e.detail || {};
    const _this = this;
    wx.showLoading({
      title: "登录中...",
      mask: true,
    });
    console.log(params, 'login>>>01');
    app.doLogin(params, () => {
      this.getUserInfo();
      _this.setData({
        hasLogin: true
      })
    });
  },

  checkLoginStatus() {
    let loginFlag = wx.getStorageSync("loginFlag");
    if (loginFlag) {
      const _this = this;
      wx.checkSession({
        success () {
          _this.getUserInfo();
        },
        fail () {
          _this.setData({
            hasLogin: false,
          });
        },
      });
    } else {
    }
  },

  getUserInfo() {
    const { userInfo } = app.globalData;
    if (userInfo) {
      this.setData({
        hasLogin: true,
        userInfo,
      });
      wx.hideLoading();
    } else {
      console.log("为空");
    }
  },

  queryMultipleNodes(){//声明节点查询的方法
    var query = wx.createSelectorQuery()//创建节点查询器 query
    query.select('.login-header').boundingClientRect()//这段代码的意思是选择Id=the-id的节点，获取节点位置信息的查询请求
    query.selectViewport().scrollOffset()//这段代码的意思是获取页面滑动位置的查询请求
    query.exec(function(res){
      res[0].top       // #the-id节点的上边界坐标
      res[1].scrollTop // 显示区域的竖直滚动位置
    })
  },

  toClick() {
    login(this)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("Event: onLoad");
    this.checkLoginStatus();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log("Event: onReady");
    var query = wx.createSelectorQuery()
    query.select('.login-header').boundingClientRect()
    query.exec(function (res) {
      console.log("1" ,res)       // #the-id节点的上边界坐
    })
  },

  /**
   * 生命周期函数--监听页面显示
   * 每次打开页面都会调用一次
   */
  onShow: function () {
    console.log(app, "Event: onShow");
    var query = wx.createSelectorQuery()
    query.select('.login-header').boundingClientRect()
    query.exec(function (res) {
      console.log("2" ,res)       // #the-id节点的上边界坐
    });
    const { userInfo } = app.globalData;
    this.setData({
      userInfo,
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   * 当 navigateTo 或底部 tab 切换时调用
   */
  onHide: function () {
    console.log("Event: onHide");
  },

  /**
   * 生命周期函数--监听页面卸载
   * 当 redirectTo 或 navigateBack 的时候调用。
   */
  onUnload: function () {
    console.log("Event: onUnload");
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
